package com.ahaoyou.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.LockSupport;


public class JMMTest {
    static Map<Integer, User> maps = new ConcurrentHashMap<>();

    static {
        maps.put(1, new User(1, "张三", 15));
        maps.put(2, new User(2, "李四", 16));
        maps.put(3, new User(3, "王五", 17));
        maps.put(4, new User(4, "孙六", 18));
    }

    public static void main(String[] args) {
//        Map<Integer, User> maps = new ConcurrentHashMap<>();
//        maps.put(1, new User(1, "张三", 15));
//        maps.put(2, new User(2, "李四", 16));
//        maps.put(3, new User(3, "王五", 17));
//        maps.put(4, new User(4, "孙六", 18));
        Object object = new Object();
        Thread thread1 = new Thread(() -> {
            System.out.println("==============thread1开始获取数据.......");
            User user = maps.get(1);
            System.out.println("==============thread1准备休眠.......");
            LockSupport.park();
            System.out.println("==============thread1休眠结束......." + maps);
            System.out.println("==============thread1......." + user);
        });

        Thread thread2 = new Thread(() -> {
            System.err.println("thread2准备清楚map中所有数据.......");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            maps.remove(1);
//            User user = maps.get(1);
//            user.setAge(88);
//            user.setName("张三1");
//            user.setId(11);
            System.err.println("thread2" + maps);
            LockSupport.unpark(thread1);
            System.err.println("thread2删除数据完成.......");
        });
        thread1.start();
        thread2.start();
    }

}

class User {
    public int id;
    public String name;
    public int age;

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}